package 链表;

import java.util.LinkedList;
import java.util.Queue;
import java.util.concurrent.DelayQueue;

public class Solution11 {
    //https://www.nowcoder.com/practice/c087914fae584da886a0091e877f2c79
    public static ListNode deleteDuplicates1 (ListNode head) {
        //思路：队列
        Queue<ListNode> queue = new LinkedList<>();
        ListNode cur = head;
        /*while (cur != null) {
            if(!queue.contains(cur)) {//节点与节点就是不同的，要比值相同
                queue.offer(cur);
            }
            cur = cur.next;
        }*/
        ListNode prev = null;
        while (cur != null) {
            if(queue.isEmpty() || prev.val != cur.val) {//peek是查看队头节点但不移除它
                queue.offer(cur);
                prev = cur;
            }
            cur = cur.next;
        }
        ListNode ret = null;
        if(!queue.isEmpty()) {
            ret = queue.poll();
        }
        ListNode tail = ret;
        while (!queue.isEmpty()) {
            tail.next = queue.poll();
            tail = tail.next;
        }
        if(tail != null) {
            tail.next = null;
        }
        return ret;
    }
    public ListNode deleteDuplicates2 (ListNode head) {
        //题解：
        //思路：直接往后遍历判断
        if(head == null) {
            return null;
        }
        ListNode prev = head;
        ListNode cur = head.next;
        while (cur != null) {
            if(cur.val != prev.val) {
                prev.next = cur;
                prev = prev.next;
            }
            cur = cur.next;
        }
        prev.next = null;
        return head;
    }
    //用新建链表的形式：
    public ListNode deleteDuplicates3 (ListNode head) {
        if(head == null) {
            return null;
        }
        ListNode reHead = new ListNode(head.val);
        ListNode prev = reHead;
        ListNode cur = head.next;
        while (cur != null) {
            if(cur.val != prev.val) {
                prev.next = new ListNode(cur.val);
                prev = prev.next;
            }
            cur = cur.next;
        }
        return reHead;
    }
    public static void main(String[] args) {
        ListNode node1 = new ListNode(1);
        ListNode node2 = new ListNode(2);
        ListNode node3 = new ListNode(2);
        node1.next = node2;
        node2.next = node3;
        deleteDuplicates1(node1);
    }
}
